$.fn.serializeObject = function () {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function () {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

$(function () {
    /* toolbar 筛选 */
    $('.toolbar .filter').click(function () {
        var value = $(this).val();
        $('#state').val(value);
        $('#table').bootstrapTable('refresh');

        //改变按钮样式
        $('.toolbar .filter').removeClass('btn-primary btn-info btn-success btn-danger btn-warning');
        $('.toolbar .filter').addClass('btn-default');
        $(this).removeClass('btn-default').addClass('btn-info');
    });
    // 列表排序功能
    $('#table-sort').click(function () {
        if ($(this).hasClass('active')) {
            $(this).removeClass('active');
            $(this).text('排序');
            $('#table tbody').sortable('disable');
            $('#table tbody').enableSelection();
            msg('已禁用排序功能')
        }
        else {
            $(this).addClass('active');
            $(this).text('排序(已启用)');
            $('#table tbody').sortable('enable');
            $('#table tbody').disableSelection();
            msg('已启用排序功能')
        }
    });
});




function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

function formatSeconds(value) {
    var theTime = parseInt(value);// 秒
    var theTime1 = 0;// 分
    var theTime2 = 0;// 小时
    if (theTime > 60) {
        theTime1 = parseInt(theTime / 60);
        theTime = parseInt(theTime % 60);
        if (theTime1 > 60) {
            theTime2 = parseInt(theTime1 / 60);
            theTime1 = parseInt(theTime1 % 60);
        }
    }
    var result = "" + parseInt(theTime) + "秒";
    if (theTime1 > 0) {
        result = "" + parseInt(theTime1) + "分" + result;
    }
    if (theTime2 > 0) {
        result = "" + parseInt(theTime2) + "小时" + result;
    }
    return result;
}



//获取指定table选中的rows
function getIds(elementId){
    elementId = elementId || 'table';
    var rows = $('#' + elementId).bootstrapTable('getSelections');
    var ids = [];
    for (var i = 0; i < rows.length; i++) {
        ids[ids.length] = rows[i].id;
    }
    return ids;
}


//提示
function msg(message, type) {
    type = type || 'info';
    var icon = 'fa-info-circle';
    switch (type) {
        case 'success':
            icon = 'fa-check';
            break;
        case 'warning':
            icon = 'fa-exclamation';
            break;
        case 'error':
			type = 'danger';
            icon = 'fa-times';
            break;

    }
    One.helpers('notify', {type: type, icon: 'fa ' + icon + 'mr-1', message: message});
    return false;
}

/**
 * 字符串截取
 * @param str
 * @param length
 * @returns {*}
 */
function substr(str, length) {
    if(str.length > length) {
        str = str.substr(0, length) + '...';
    }
    return str;
}

